<?php
class Powers extends BaseBean{

    protected $_table= "tb_powers";
    protected $_primarykey= "id";
    protected $_mustFields =array("code"=>"权限编号","name"=>"权限名称");
    protected $_uniqueFields =array("code"=>"权限编号");

    public  static function getAllAvailablePowers(){
    	
    	//尝试从缓存文件加载
    	$filename=APP_PATH.DIRECTORY_SEPARATOR."powers_".date("Ymd").".json";

    	//如果文件不存在则从数据库查找并写入文件

    	if(!file_exists($filename)){
    		$fp = fopen($filename, "a+");
			if (is_writable($filename)) {
                $powerArr = array(); 
				$powers  = AbstractBean::LoadBean("Powers");
				$rows = $powers->fetchArray(new SqlFilter("state",EQUALS,1));
                if($rows!=null){
                    for($i=0;$i<count($rows);$i++){
                        $controller_url = $rows[$i]["controller_url"];
                        if($controller_url!=null &&  $controller_url!=null){
                            $arr = explode(",",$controller_url);
                            for($j=0;$j<count($arr);$j++){
                                $powerArr[$arr[$j]] =  $rows[$i]["code"];
                            }
                        }
                    }
                }
	        	fwrite($fp,json_encode($powerArr));
	        }
	        
	        //如果存在旧的缓存文件则删除
	    	$oldFile=APP_PATH.DIRECTORY_SEPARATOR."powers_".date("Ymd",strtotime("-1 day")).".json";
	        if(file_exists($oldFile)){
	        	unlink($oldFile);
	        }
    	}

    	$json = file_get_contents($filename);
    	if($json!=null ||  $json!=""){
    		return (array)json_decode($json);
    	}
	}

}
?>